package com.ty.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.xml.internal.bind.v2.TODO;
import com.ty.common.redis.CacheUtil;
import com.ty.common.util.StringUtil;
import com.ty.common.utils.MapUtils;
import com.ty.common.utils.R;
import com.ty.common.utils.SmsUtil;
import com.ty.entity.Customer;
import com.ty.entity.Orders;
import com.ty.service.CustomerService;
import com.ty.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author 天宇宇宇宇宇
 * @Data 2020/12/2 15:55
 */
@RestController
@RequestMapping(value = "customerPhone")
@Slf4j
public class CustomerPhoneController {
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private OrdersService ordersService;

    /**
     * 计算出两地距离的金额
     * @param lng
     * @param lat
     * @param oldLng
     * @param oldLat
     * @return
     */
    @GetMapping(value = "getMoney")
    public R getMoney(
            @RequestParam(value = "lng") double lng,
            @RequestParam(value = "lat") double lat,
            @RequestParam(value = "oldLng") double oldLng,
            @RequestParam(value = "oldLat") double oldLat
    ){
        log.info("传输的数据为{},{},{},{}",oldLat, oldLng, lat, lng);
        //计算出两地相距的距离(米)
        double algorithm = MapUtils.getDistance(oldLat, oldLng, lat, lng);
        //每公里10元
        int money = (int) (Math.rint(algorithm) / 1000 * 10);
        return R.ok().put("cost", money);
    }

    /**
     * 判断是否登录
     * @return
     */
    @GetMapping(value = "isLogin")
    public R isLogin(
            HttpSession session
    ){
        String customerPhone = (String) session.getAttribute("user");
        log.info("手机号为{}", customerPhone);
        if (StringUtil.isEmpty(customerPhone)) return R.error();
        boolean b = CacheUtil.hasKey(customerPhone);
        log.info("结果为{}",b);
        return b ? R.ok() : R.error("用户未登录");
    }

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @PostMapping(value = "getCode")
    public R getCode(
            @RequestParam(value = "mobile") String mobile
    ){
        //随机验证码
        String fourRandom = SmsUtil.getFourRandom();
        // TODO 固定的验证码返回
        fourRandom = "1234";
        // 发送验证码
        /*smsUtil.sendSms(mobile, fourRandom);*/
        //k: 手机号 v:验证码
        CacheUtil.setString(mobile, fourRandom);
        //设置过期秒数
        boolean b = CacheUtil.setExp(mobile, 300);
        log.info("设置过期秒数是否成功{}, 验证码为{}", b, fourRandom);
        return R.ok();
    }

    /**
     * 验证 验证码是否正确
     * @param mobile
     * @param smsCode
     * @return
     */
    @PostMapping(value = "verification")
    public R verification(
            @RequestParam(value = "mobile") String mobile,
            @RequestParam(value = "smsCode") String smsCode,
            HttpSession session
    ){
        if (StringUtil.isEmpty(mobile) || StringUtil.isEmpty(smsCode)) return R.error("手机号或验证码不合法");
        //从session中取出 k v
        String string = CacheUtil.getString(mobile);
        boolean b = smsCode.equals(string);
        //mobile smsCode相同 则将手机号存储到session中,作为已登录的依据,redis也存一份
        if (b) {
            session.setAttribute("user", mobile);
            CacheUtil.setString(mobile, smsCode);
            //将用户添加到用户表
            Customer customer = this.customerService.getOne(new QueryWrapper<Customer>().eq("phone", mobile));
            if (customer == null && customer.getId() == null){
                Customer customer1 = new Customer();
                customer1.setPhone(mobile);
                this.customerService.saveCustomer(customer1);
            }

        }
        return b ? R.ok() : R.error("验证码不合法");
    }
}
